DEBUG = 1
CC = gcc
COPT = -Wall -O2 -include "./film.h" -DDEBUG=$(DEBUG)

help:
	@grep '^[^	 ][^:]*:' ./Makefile | sed 's/:.*$$//'
clean:
	@rm -rf *.o *.a tmp/* *.test film
	@rm -rf *.test
test: $(FILES:%=%.test)
	make $<
%.print: %.c
	gcc -E $< -DTEST_MODE -include "./film.h"
%.test: %.c film.h
	rm -f $@;\
	gcc -o $@ $< util/test.c -DTEST_MODE -include "./film.h" -include "util/test.h";\
	[ -e $@ ] && util/test $@

film: film.c tmp
	$(CC) $(COPT) -o $@ $<

tmp: tmp/engine.h tmp/exceptions.h tmp/constants.h tmp/c.h
tmp/exceptions.h: data/exceptions
	perl -e '$$c=0; while(<>){if(/^([^:]+): *(.+)/){print "X($$1, $$c, \"$$2\")\n";$$c++}}' $< > $@
tmp/constants.h: data/constants
	perl -e 'while (<>) { if (/([^ ]+) +(.+)/) { my $$a = $$1; my $$b = $$2; $$a =~ s/"/\\"/g; print("X(\"$$a\", $$b)\n"); }}' $< > $@
tmp/c.h: data/c
	perl -e 'while(<>){if(/^([^ ]+) *([^ ]+) *(\d+) *(\d+) *(immediate)?/){print"X(\"$$1\", $$2, $$3, $$4, ".($$5 ? "FL_IMMEDIATE_BIT": "FL_NONE_BIT").", \"ccall$$3->$$4\")\n"}}' data/c > $@
tmp/engine.h: egenerate.pl
	./egenerate.pl > $@
